/*
 * CS_LOCATION_MODIFY.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */

#include "../../User.h"
#include "CS_LOCATION_MODIFY.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/Party.h"

namespace ProjectZ { namespace NCommand { namespace NAction {

static void PVPAILocationModify(User * pUser, int GID, NLogic::Space * pSpace, int8_t type, int16_t x, int16_t y, int16_t degree, int8_t ismonster)
{
	NLogic::Party * pParty = pSpace->GetParty();
	if (NULL == pParty)
		return;

	User * pAI = pParty->GetPVPDelayUser();
	if (NULL == pAI)
		return;

	if (GID != pAI->GetUserSeq())
		return;

	NLogic::Battle * pAIBattle = pAI->GetBattle();
	if (NULL == pAIBattle)
		return;

	pAIBattle->GetData().x	= x;
	pAIBattle->GetData().y	= y;
	pAIBattle->GetData().degree	= degree;

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pNoti << GID << type << x << y << degree << ismonster;
	pNoti->MakePacket(CMD_SN_LOCATION_MODIFY);

	pSpace->SendAllExceptMe(pNoti, pUser);
}

static void PKAILocationModify(User * pUser, int GID, NLogic::Space * pSpace, int8_t type, int16_t x, int16_t y, int16_t degree, int8_t ismonster)
{
	NLogic::Battle * pAIBattle = pSpace->FindBattle(GID);
	if (NULL == pAIBattle)
		return;

	pAIBattle->GetData().x	= x;
	pAIBattle->GetData().y	= y;
	pAIBattle->GetData().degree	= degree;

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pNoti << GID << type << x << y << degree << ismonster;
	pNoti->MakePacket(CMD_SN_LOCATION_MODIFY);

	pSpace->SendAllExceptMe(pNoti, pUser);
}

void CS_LOCATION_MODIFY::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * 위치 보정 용 패킷..
		CS_LOCATION_MODIFY	0x0408	6
					4	unsigned int	GID
					1	char	위치전송타입(0 부터 시작)
					2	posx	x 좌표
					2	posy	y 좌표
					2	degree	방향 벡터
					1	char	몬스터여부 (0 : 주인공 1: 몬스터 2: 주인공소환, 3: 몬스터 소환)

		위치 보정 용 패킷..
		SN_LOCATION_MODIFY	0x0409	6
					4	unsigned int	GID
					1	char	위치전송타입(0 부터 시작)
					2	posx	x 좌표
					2	posy	y 좌표
					2	degree	방향 벡터
					1	char	몬스터여부 (0 : 주인공 1: 몬스터 2: 주인공소환, 3: 몬스터 소환)
	 *
	 */
	JJAEHUNYLOG("[CHANNEL] CS_LOCATION_MODIFY::ONEXECUTE");

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		return;
	}

	PREPARE_PACKET;

	int GID = 0;
	int8_t  type = 0;
	int16_t x = 0;
	int16_t y = 0;
	int16_t degree = 0;
	int8_t  ismonster = 0;

	*pPacket >> GID >> type >> x >> y >> degree >> ismonster;

	int targetgid = 0;

	if( ismonster == 0 )
	{
		// AI 위치 정보 보정
		if (GID != pUser->GetUserSeq())
		{
			if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_USER
					|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::NEW_PVP_ROOM)
			{
				PVPAILocationModify(pUser, GID, pSpace, type, x, y, degree, ismonster);
				return;
			}

			if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::BATTLEROYAL)
			{
				PKAILocationModify(pUser, GID, pSpace, type, x, y, degree, ismonster);
				return;
			}

			return;
		}

		targetgid = pUser->GetUserSeq();

		if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::BATTLEROYAL && pUser->GetEnumState() == User::EnumState::BATTLE_ROYAL_BATTLE)
		{
			pUser->GetBattleRoyalBattle()->GetData().x = x;
			pUser->GetBattleRoyalBattle()->GetData().y = y;
			pUser->GetBattleRoyalBattle()->GetData().degree = degree;

			DEBUGLOG("GetBattleRoyalBattle CS_LOCATION_MODIFY x : %d", x);
		}
		else
		{
			pUser->GetBattle()->GetData().x = x;
			pUser->GetBattle()->GetData().y = y;
			pUser->GetBattle()->GetData().degree = degree;
		}
	}
	else if( ismonster == 1 )
	{
		targetgid = GID;

		NLogic::Monster * pMonster = pUser->GetSpace()->GetMonster(targetgid);
		if (NULL != pMonster)
		{
			pMonster->GetData().x = x;
			pMonster->GetData().y = y;
			pMonster->GetData().degree = degree;
		}
	}
	else if( ismonster == 2 )
	{
		targetgid = GID;

		NLogic::Monster * pMonster = pUser->GetSpace()->GetMonster(targetgid);
		if (NULL != pMonster)
		{
			pMonster->GetData().x = x;
			pMonster->GetData().y = y;
			pMonster->GetData().degree = degree;
		}
	}
	else if( ismonster == 3 )
	{
		targetgid = GID;
		NLogic::Monster * pMonster = pUser->GetSpace()->GetMonster(targetgid);
		if (NULL != pMonster)
		{
			pMonster->GetData().x = x;
			pMonster->GetData().y = y;
			pMonster->GetData().degree = degree;
		}
	}

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pNoti << targetgid << type << x << y << degree << ismonster;
	pNoti->MakePacket(CMD_SN_LOCATION_MODIFY);

	pSpace->SendAllExceptMe(pNoti, pUser);

	// check latency
//	if( pSpace->bFristMovePacket == false )
//		pSpace->bFristMovePacket = true;
//
//	CheckLatency(pUser, x, y);
}

void CS_LOCATION_MODIFY::CheckLatency(User *pUser, int x, int y)
{
//	if( pUser->player_info.Locate != ProjectZ::PL_PK &&
//		pUser->player_info.Locate != ProjectZ::PL_PVP_FRIENDLY &&
//		pUser->player_info.Locate != ProjectZ::PL_PVP_QUICK )
//	{
//		return;
//	}
//
//	if( pUser->latency_info.state == LATENCYFLAG_OFF )
//		return;
//
//	// set state to on
//	pUser->latency_info.state = LATENCYFLAG_OFF;
//
//	// remove from latency uses
//	NGame::CBaseSpace *pSpace = pUser->GetSpace();
//	map<int, User *>::iterator itr = pSpace->pLatencyUser.find(pUser->GetGID());
//	if( itr != pSpace->pLatencyUser.end() )
//		pSpace->pLatencyUser.erase(itr);
//
//	// send packet
//	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
//#if 0
//	pNoti->MsgCat("%c", pSpace->pLatencyUser.size());
//
//	itr = pSpace->pLatencyUser.begin();
//	for( ; itr != pSpace->pLatencyUser.end() ; itr++ )
//	{
//		pNoti->MsgCat("%d", (*itr).first);
//	}
//#else
//	// always one
//	*pNoti << (char)1 << pUser->GetGID();
//	//pNoti->MsgCat("%c%d", 1, pUser->GetGID(), x, y);
//#endif
//
//	pNoti->MakePacket(CMD_SC_BATTLE_LATENCY_STOP);
//
//	pSpace->SendToMessageExceptMe(pNoti, pUser->GetGID());
//
//	FILELOG("SS:%s: latency state off gid %d", __func__, pUser->GetGID());
}


} /*NAction*/ } /*NCommand*/ } /*ProjectZ*/
